import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器模式：机构型设计模式
 */
public class FilterPattern {
    public static void main(String[] args) {
        Person person = new Person("张三", "男", "20");
        Person person1 = new Person("李四", "男", "25");
        Person person2 = new Person("王五", "女", "18");
        Person person3 = new Person("赵六", "男", "18");
        List<Person> persons = new ArrayList<>();
        persons.add(person);
        persons.add(person1);
        persons.add(person2);
        persons.add(person3);
        //年龄18
        List<Person> agePersons = new AgeFilter().doFilter(persons);
        System.out.println(agePersons);
        //男
        List<Person> sexPersons = new SexFilter().doFilter(persons);
        System.out.println(sexPersons);
        //年龄18的男性
        List<Person> ageSexPersons = new SexAndAgeFilter().doFilter(persons);
        System.out.println(ageSexPersons);

    }
}
//业务类，Person
class Person{
    String name;
    String sex;
    String age;
    public Person(String name,String sex,String age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
//过滤器接口
interface Filter{
    //过滤
    List<Person> doFilter(List<Person> persons);
}

//年龄过滤器
class AgeFilter implements Filter{
    @Override
    public List<Person> doFilter(List<Person> persons) {
        List<Person> agePersons = new ArrayList<>();
        for ( Person person : persons){
            if (person.getAge().equals("18")){
                agePersons.add(person);
            }
        }
        return agePersons;
    }
}

//性别过滤器
class SexFilter implements Filter{
    @Override
    public List<Person> doFilter(List<Person> persons) {
        List<Person> sexPersons = new ArrayList<>();
        for ( Person person : persons){
            if (person.getSex().equals("男")){
                sexPersons.add(person);
            }
        }
        return sexPersons;
    }
}
//性别和年龄过滤器
class SexAndAgeFilter implements Filter{
    @Override
    public List<Person> doFilter(List<Person> persons) {
        List<Person> people = new SexFilter().doFilter(persons);
        return new AgeFilter().doFilter(people);
    }
}

